﻿using MobilesStore.Domain.Abstract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobilesStore.WebUI.Models;
using MobilesStore.Domain.Entities;

namespace MobilesStore.WebUI.Controllers
{
    public class ProductController : Controller
    {
        public int PageSize = 16;
        private IProductsRepository ProductsRepository;

        public ProductController(IProductsRepository productRepository)
        {
            this.ProductsRepository = productRepository;
        }


        public FileContentResult GetImage(int productID)
        {
            Product product = ProductsRepository.Products.FirstOrDefault(p => p.ProductID == productID);

            if (product != null)
            {
                return File(product.ImageData, product.ImageMimeType);
            }
            else {
                return null;
            }
        }

        public ActionResult List(String category, int page = 1)
        {
            ProductsListViewModel model = new ProductsListViewModel
            {
                Products = ProductsRepository.Products
                .Where(p=>category==null || p.ProductCategory==category)
                .OrderBy(e => e.ProductID)
                .Skip((page - 1) * PageSize)
                .Take(PageSize),
                pagingInfo = new PagingInfo { 
                    CurrentPage=page,ItemsPerPage=PageSize,
                    TotalItems=category==null?ProductsRepository.Products.Count():ProductsRepository.Products.Where(p=>p.ProductCategory==category).Count()},
                CurrentCatgory = category
            };
            return View(model);
        }

        public ViewResult Detail(int productID)
        {
            Product product = ProductsRepository.Products
                .FirstOrDefault(p => p.ProductID == productID);
            return View(product);
        }
    }
}
